<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ssit.answer.dao.${packageDir}.${className}Dao">

    <resultMap id="${className}" type="com.ssit.answer.domain.${packageDir}.${className}">
    <#assign hasDeleteFlagColumn =  false />
    <#list columns as column>
        <#if column.columnName == primaryKey >
            <#assign column_id =  column.columnName />
            <#assign property_id = column.propertyName />
            <id property="id" column="${column.columnName}"/>
        <#else>
            <result property="${column.propertyName}" column="${column.columnName}"/>
        </#if>
    </#list>
    </resultMap>
    
    <!--表所有字段的sql -->
    <sql id="Base_Column_SQL">
        <#list columns as column>${column.columnName}<#if column_has_next>,</#if></#list>
    </sql>
    <sql id="Alias_Column_SQL">
        <#list columns as column>t.${column.columnName}<#if column_has_next>,</#if></#list>
    </sql>

    <!-- 保存对象-->
    <insert id="insert" parameterType="com.ssit.answer.domain.${packageDir}.${className}">
        INSERT INTO ${tableName}(<include refid="Base_Column_SQL"></include>)
        VALUES(
        <#list columns as column>
            <#if column.columnName == primaryKey>
            ${r"#{id}"}<#if column_has_next>,</#if>
            <#else >
            ${r"#{"}${column.propertyName}${r"}"}<#if column_has_next>,</#if>
            </#if>
        </#list>
        )
    </insert>

    <!-- 修改对象 -->
    <update id="update" parameterType="com.ssit.answer.domain.${packageDir}.${className}">
        UPDATE ${tableName} t
        <set>t.${column_id} = ${r"#{id}"},
        <#list columns as column>
            <#if column.columnName == primaryKey || column.columnName?contains("create_user_id") || column.columnName?contains("create_time") >
            <#else>
                <if test="@com.ssit.answer.utils.BaseUtil@isNotEmpty( ${column.propertyName} )">t.${column.columnName} = ${r"#{"}${column.propertyName}${r"}"},</if>
            </#if>
        </#list>
        </set>
        WHERE t.${column_id} = ${r"#{id}"}
    </update>

    <!-- 根据主键查询对象-->
    <select id="getById" parameterType="String" resultMap="${className}">
        SELECT
            <include refid="Alias_Column_SQL"></include>
        FROM ${tableName} t
        WHERE t.${column_id} = ${r"#{id}"}
        LIMIT 1
    </select>

    <!-- 根据多条件查询对象-->
    <select id="find${className}" parameterType="com.ssit.answer.domain.${packageDir}.${className}" resultMap="${className}">
        SELECT
            <include refid="Alias_Column_SQL"></include>
        FROM ${tableName} t
        <where> <#if hasDeleteFlagColumn??>t.delete_flag=0</#if>
    <#list columns as column>
        <#if column.columnName == primaryKey >
            <if test="@com.ssit.answer.utils.BaseUtil@isNotEmpty( id )"> AND t.${column_id} = ${r"#{id}"}</if>
        <#elseif column.columnName?contains("create_user_id") || column.columnName?contains("create_time") ||column.columnName?contains("update_user_id") ||column.columnName?contains("update_time") ||column.columnName?contains("delete_flag")>
        <#else>
            <if test="@com.ssit.answer.utils.BaseUtil@isNotEmpty( ${column.propertyName} )"> AND t.${column.columnName} = ${r"#{"}${column.propertyName}${r"}"}</if>
        </#if>
    </#list>
        </where>
        ORDER BY t.create_time DESC LIMIT 1
    </select>

    <!-- 查询对象列表-->
    <select id="list${className}" parameterType="com.ssit.answer.domain.${packageDir}.${className}" resultMap="${className}">
        SELECT
            <include refid="Alias_Column_SQL"></include>
        FROM ${tableName} t
        <where> <#if hasDeleteFlagColumn??>t.delete_flag=0</#if>
    <#list columns as column>
        <#if column.columnName == primaryKey >
            <if test="@com.ssit.answer.utils.BaseUtil@isNotEmpty( id )"> AND t.${column_id} = ${r"#{id}"}</if>
        <#elseif column.columnName?contains("create_user_id") || column.columnName?contains("create_time") ||column.columnName?contains("update_user_id") ||column.columnName?contains("update_time") ||column.columnName?contains("delete_flag") >
        <#else>
            <if test="@com.ssit.answer.utils.BaseUtil@isNotEmpty( ${column.propertyName} )"> AND t.${column.columnName} = ${r"#{"}${column.propertyName}${r"}"}</if>
        </#if>
    </#list>
        </where>
        ORDER BY t.create_time DESC 
    </select>

</mapper>